import requests
import re
import xlwt

url1 = 'https://zhaopin.baidu.com/api/qzasync?query=%E6%96%87%E5%91%98&zp_fr=aladdin-5463-zp_exact_new&city_sug=%E6%B7%B1%E5%9C%B3&city=%E6%B7%B1%E5%9C%B3&is_adq=1&pcmod=1&token=%3D%3DgmgGrqX2K1JmIlmZmayRWlut2ZWdYYkJWapppYpxpa&pn=0&rn=20'
headers = {
    'Accept': 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Cookie': 'guid=0c0b291cb3b2ab98da35c43202ee9904; nsearch=jobarea%3D%26%7C%26ord_field%3D%26%7C%26recentSearch0'
              '%3D%26 '
              '%7C%26recentSearch1%3D%26%7C%26recentSearch2%3D%26%7C%26recentSearch3%3D%26%7C%26recentSearch4%3D%26%7C'
              '%26collapse_expansion%3D; adv=adsnew%3D1%26%7C%26adsresume%3D1%26%7C%26adsfrom%3Dhttps%253A%252F%252Fsp0'
              '.baidu.com%252F9q9JcDHa2gU2pMbgoY3K%252Fadrc.php%253Ft'
              '%253D06KL00c00fDewkY0nk4M00uiAsjzvv4X00000av4p7C00000TgwuAp'
              '.THYdnyGEm6K85yF9pywd0ZnqmvPBuHKWrycsnjRvPhDvu6Kd5HcLfWnsf1FAnWfYnWbznHRsnbPDPj9Knb'
              'R1wHmYP17D0ADqI1YhUyPGujY1n1RknWf1P1DYFMKzUvwGujYkP6K-5y9YIZK1rBtEIZF9mvR8PH7JUvc8mvqVQLwzmyP-QMKCTjq9uZP'
              '8IyYqnW0sPjc3nBu9pM0qmR9inAPcHHunXH-YmHPwIR4RwM7Bnb-dyHc4IDs1Rh4nnY4_m-n4IvN_rHFPpHNEuDTkTd0zyM-BnW04yydA'
              'T7GcNMI-u1YqFh_qPWmvuhD3uAuWnyc3uWmvn1csnAF-uAw-PHw9uH--PAR0mLFW5HRvrHDv%2526tpl%253Dtpl_11534_21264_17382'
              '%2526l%253D1516812589%2526attach%253Dlocation%25253D%252526linkName%25253D%252525E6%252525A0%25252587%2525'
              '25E5%25252587%25252586%252525E5%252525A4%252525B4%252525E9%25252583%252525A8-%252525E6%252525A0%25252587%2'
              '52525E9%252525A2%25252598-%252525E4%252525B8%252525BB%252525E6%252525A0%25252587%252525E9%252525A2%2525259'
              '8%252526linkText%25253D%252525E3%25252580%25252590%252525E5%25252589%2525258D%252525E7%252525A8%2525258B%2'
              '52525E6%25252597%252525A0%252525E5%252525BF%252525A751Job%252525E3%25252580%25252591-%25252520%252525E5%25'
              '2525A5%252525BD%252525E5%252525B7%252525A5%252525E4%252525BD%2525259C%252525E5%252525B0%252525BD'
              '%252525E5%2 '
              '525259C%252525A8%252525E5%25252589%2525258D%252525E7%252525A8%2525258B%252525E6%25252597%252525A0'
              '%252525E5% '
              '252525BF%252525A7%2521%252526xp%25253Did%2528%25252522m3351243714_canvas%25252522%2529%2525252FDIV%2525255B'
              '1%2525255D%2525252FDIV%2525255B1%2525255D%2525252FDIV%2525255B1%2525255D%2525252FDIV%2525255B1%2525255D%252'
              '5252FDIV%2525255B1%2525255D%2525252FH2%2525255B1%2525255D%2525252FA%2525255B1%2525255D%252526linkType%25253D'
              '%252526checksum%25253D22%2526ie%253Dutf-8%2526f%253D8%2526tn%253Dbaidu%2526wd%253D51job%2526rqlang%253Dcn%25'
              '26inputT%253D1781%26%7C%26adsnum%3D2004282; search=jobarea%7E%60040000%7C%21ord_field%7E%600%7C%21recentSear'
              'ch0%7E%60040000%A1%FB%A1%FA000000%A1%FB%A1%FA0000%A1%FB%A1%FA00%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1'
              '%FA99%A1% '
              'FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA9%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1%FA0%A1%FB%A1%FA%CE%C'
              '4%D4%B1%A1%FB%A1%FA2%A1%FB%A1%FA1%7C%21recentSearch1%7E%60040000%A1%FB%A1%FA000000%A1%FB%A1%FA0000%A1%FB%A1%'
              'FA00%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA9%A1%FB%A1'
              '%FA02%A1%FB%A1%FA%A1%FB%A1%FA0%A1%FB%A1%FA%CE%C4%D4%B1%A1%FB%A1%FA2%A1%FB%A1%FA1%7C%21recentSearch2%7E'
              '%60040 '
              '000%A1%FB%A1%FA000000%A1%FB%A1%FA0000%A1%FB%A1%FA00%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1%FA99%A1%FB%A1%FA99%A1'
              '%FB%A1%FA99%A1%FB%A1%FA99%A1%FB%A1%FA9%A1%FB%A1%FA99%A1%FB%A1%FA%A1%FB%A1%FA0%A1%FB%A1%FA%A1%FB%A1%FA2'
              '%A1%FB '
              '%A1%FA1%7C%21collapse_expansion%7E%601%7C%21',
    'Host': 'search.51job.com',
    'Referer': 'https://search.51job.com/list/040000,000000,0000,00,9,99,%25E6%2596%2587%25E5%2591%2598,2,'
               '4.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99'
               '&ord_field=0&dibiaoid=0&line=&welfare=',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 '
                  'Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest'}
form_data = {
    'rand': '0.6776675166671151',
    'jsoncallback': 'jQuery183026759138383872627_1584323743007',
    'jobarea': '040000',
    'district': '040300',
    'funtype': '0000',
    'industrytype': '00',
    'issuedate': '9',
    'providesalary': '99',
    'keyword': '文员',
    'keywordtype': '2',
    'postchannel': '0000',
    'workyear': '99',
    'cotype': '99',
    'degreefrom': '99',
    'jobterm': '99',
    'companysize': '99',
    'address': '',
    'line': '',
    'ord_field': '0',
    'dibiaoid': '0',
    'welfare': '',
    'highlight': '',
    '_': '1584323792229'
}
# form_data['pager.pageNumber'] = times
url2 = 'https://zhaopin.baidu.com/api/pclistasp?query=%E6%96%87%E5%91%98&cityname=%E6%B7%B1%E5%9C%B3&pn=0'
url3 = 'https://zhaopin.baidu.com/quanzhi?query=%E6%96%87%E5%91%98&city=%E6%B7%B1%E5%9C%B3&&ua_from=pc'
url4 = 'https://search.51job.com/list/040000,000000,0000,00,9,99,%25E6%2596%2587%25E5%2591%2598,2,' \
       '1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99' \
       '&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line' \
       '=&specialarea=00&from=&welfare= '
r = requests.get(url4, headers=headers)
r.encoding = 'gbk'
# print(r.text)

workBook = xlwt.Workbook(encoding='utf-8')
workSheet = workBook.add_sheet('r')
colName = ['岗位名称', '公司名称', '公司地点', '薪资', '发布时间']
for col in range(len(colName)):
    workSheet.write(0, col, colName[col])
# </div>
#
#         <div class="el">
#         <p class="t1 ">
#             <em class="check" name="delivery_em" onclick="checkboxClick(this)"></em>
#             <input class="checkbox" type="checkbox" name="delivery_jobid" value="113410787" jt="0" style="display:none" />
#             <span>
#                 <a target="_blank" title="办公室文员" href="https://jobs.51job.com/shenzhen/113410787.html?s=01&t=0"  onmousedown="">
#                     办公室文员                </a>
#             </span>
#                                                                     </p>
#         <span class="t2"><a target="_blank" title="大金空调技术（中国）有限公司广州分公司" href="https://jobs.51job.com/all/co256098.html">大金空调技术（中国）有限公司广州...</a></span>
#         <span class="t3">深圳</span>
#         <span class="t4">3.5-4.5千/月</span>
#         <span class="t5">03-13</span>
#     </div>

# 筛选出所有的信息
info = re.findall('<div class="el">(.*?)</div>', r.text, re.S)
line = 1
print(info)
for one in info:
    # print(one)
    # data = re.findall('<input class="checkbox"(.*?)</span>', one, re.S)
    # print(data)
    # for i in data:
    job = re.findall('<a target="_blank" title="(.*?)"', one, re.S)[0].strip()
    print(job)
    workSheet.write(line, 0, job)
    company = re.findall('<span class="t2"><a target="_blank" title="(.*?)"', one, re.S)[0].strip()
    print(company)
    workSheet.write(line, 1, company)
    address = re.findall('<span class="t3">(.*?)</span>', one, re.S)[0].strip()
    print(address)
    workSheet.write(line, 2, address)
    salary = re.findall('<span class="t4">(.*?)</span>', one, re.S)[0].strip()
    print(salary)
    workSheet.write(line, 3, salary)
    times = re.findall('<span class="t5">(.*?)</span>', one, re.S)[0].strip()
    print(times)
    workSheet.write(line, 4, times)
    line += 1
workBook.save('./python.xls')
